﻿using HKSdkLibrary;
using OSSUtility;
using Ozone.Device;
using Rubyer;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static ServiceStack.LicenseUtils;

namespace Ozone.Views.Video
{
    public class HKCam
    {
        public HKSdkMethods hKSdkMethods;//**画面显示唯一标识**
        public int m_lRealHandle = -1;//**移动唯一标识**
        public int m_userID = -1;//**拍照/回放唯一标识**
        public string ErrorMessage;
        public CancellationTokenSource ctsCamErrorMessage;
        public bool IsBig = false;
        public int nBigNum;
        public HKCam()
        {

        }
        public HKCam(string ipAddress, string password, string title,int nTagNum, UCVideoMonitor uCVideoMonitor) 
        {
            hKSdkMethods = new HKSdkMethods();
            hKSdkMethods.InitHKSdk();
            m_lRealHandle = LoginAndPreview(ipAddress, password, title, nTagNum, uCVideoMonitor);
        }
        //登录预览
        public int LoginAndPreview(string ipAddress, string password,string title,int nTagNum, UCVideoMonitor uCVideoMonitor)
        {
            uCVideoMonitor.TagC(title, nTagNum);
            if (hKSdkMethods.Login(ipAddress, password, out m_userID, out ErrorMessage))
            {
                m_lRealHandle = hKSdkMethods.Preview(uCVideoMonitor.pageVideo.pictureBox, m_userID, out ErrorMessage);
            }
            SetCamTime(hKSdkMethods,DateTime.Now,m_userID);
            //CheckErrorM();
            return m_lRealHandle;
        }
        //移动
        public void CamMoveLURD(string sType,int nMoveNum, uint isstop)
        {
            int nOp;
            int nIndex = nMoveNum - 1;
            if (isstop == 0)
                nOp = 1;
            else if (isstop == 1)
                nOp = 0;
            else
                return;
            if (IsBig)
                nIndex = nBigNum;
            sType = sType.ToLower();
            switch (sType)
            {
                case "left":
                    DeviceCenter.Entity.uCVideoMonitors[nIndex].LeftArrow.Opacity = nOp;
                    hKSdkMethods.MoveLeft(true, isstop, 7,m_lRealHandle);    //左移
                    break;
                case "right":
                    DeviceCenter.Entity.uCVideoMonitors[nIndex].RightArrow.Opacity = nOp;
                    hKSdkMethods.MoveRight(true, isstop, 7, m_lRealHandle);  //右移
                    break;
                case "up":
                    DeviceCenter.Entity.uCVideoMonitors[nIndex].UpArrow.Opacity = nOp;
                    hKSdkMethods.MoveUp(true, isstop, 7, m_lRealHandle);     //上移
                    break;
                case "down":
                    DeviceCenter.Entity.uCVideoMonitors[nIndex].DownArrow.Opacity = nOp;
                    hKSdkMethods.MoveDown(true, isstop, 7,m_lRealHandle);    //下移
                    break;
            }
        }
        //设置时间
        public void SetCamTime(HKSdkMethods hKSdkMethods, DateTime dateTime, int m_UserID)
        {
            try
            {
                hKSdkMethods.SetTime(dateTime, m_UserID, out ErrorMessage);
            }
            catch(Exception e)
            {

            }
        }
        //分析相机错误信息（未启用）
        public void CheckErrorM()
        {
            ctsCamErrorMessage = new CancellationTokenSource();
            Task.Run(async () =>
            {
                while (!ctsCamErrorMessage.IsCancellationRequested)
                {
                    if (!string.IsNullOrEmpty(ErrorMessage))
                    {
                        //错误处理逻辑

                        ErrorMessage = "";
                        await Task.Delay(500);
                    }
                }
            }, ctsCamErrorMessage.Token);
        }
    }
}